package io.renren.modules.udp.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.modules.udp.entity.R5rmItemEntity;
import io.renren.modules.udp.service.R5rmItemService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserEntity;
import static io.renren.common.utils.ShiroUtils.getUserId;


/**
 * 项目基本信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-01-15 15:40:11
 */
@RestController
@RequestMapping("udp/r5rmitem")
public class R5rmItemController {
    @Autowired
    private R5rmItemService r5rmItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("udp:r5rmitem:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = r5rmItemService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前登录人的项目
     * @return
     */
    @RequestMapping("/nodeList")
    @RequiresPermissions("udp:r5rmitem:nodeList")
    public R nodeList() {
        Map<String,Object> params = new HashMap<>();
        String username=getUserEntity().getUsername();
        params.put("it_owner", getUserEntity().getUsername());
        List<R5rmItemEntity> entities=r5rmItemService.selectList(new EntityWrapper<R5rmItemEntity>()
                .in("it_owner",username)
                .or()
                .like(StringUtils.isNotBlank(getUserEntity().getUsername()),"it_udfchar01", username));

        return R.ok().put("r5rmItem",entities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{itId}")
    @RequiresPermissions("udp:r5rmitem:info")
    public R info(@PathVariable("itId") Integer itId){
		R5rmItemEntity r5rmItem = r5rmItemService.selectById(itId);

        return R.ok().put("r5rmItem", r5rmItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("udp:r5rmitem:save")
    public R save(@RequestBody R5rmItemEntity r5rmItem){
		r5rmItemService.insert(r5rmItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("udp:r5rmitem:update")
    public R update(@RequestBody R5rmItemEntity r5rmItem){
		r5rmItemService.updateById(r5rmItem);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("udp:r5rmitem:delete")
    public R delete(@RequestBody Integer[] itIds){
		r5rmItemService.deleteBatchIds(Arrays.asList(itIds));

        return R.ok();
    }

}
